function [UHprob,p_uh_new] = UpdateUHProb2(p_uh_old,States,AgeState,Action,pOld)


% Fields with sugarcane:
NeverRep = ~any(States.SingleStateIdx ~= 0,2);

FieldsUH = (any(AgeState >= 2, 2) | NeverRep );
% Fields with known replanting activity:
FieldsObservedRep = any( (Action == 1 & AgeState >=2 ) | Action == - 1, 2);
SugarInitialButRep = (AgeState(:,3) == 0 & any(AgeState >= 2, 2));
% Fields that could have undergone unobserved replanting:
FieldsPossibleUnobRep = (FieldsUH & ~FieldsObservedRep & ~SugarInitialButRep);
%FieldsPossibleUnobRep = (FieldsUH & ~FieldsObservedRep);

IdxUnobRep  = States.SingleStateIdx(FieldsPossibleUnobRep,:);
DummyAgeUnobRep = (AgeState(FieldsPossibleUnobRep,:) >= 2); 
RegionsUnobRep  = States.RegionsForExoStates(FieldsPossibleUnobRep);

Regions = unique(States.RegionsForExoStates);
Nr = length(Regions);

ProbInaction = ones(sum(FieldsPossibleUnobRep),1);

for r = Regions'
    InacProb = pOld{r}(:,1);
    % Next two lines are a way of bypassing the fact that before replanting
    % fields already in cane don't have observed age, so we just input a
    % value for the single state idx, but this is has no effect since all
    % those values before the first replanting will have prob 1.
    T = IdxUnobRep(RegionsUnobRep == r,3:end-1);
    T(T==0) = 1; 
    InacProb = InacProb(T);
    SelDummyAge = DummyAgeUnobRep(RegionsUnobRep == r,3:end-1);
    Prob = ones(size(SelDummyAge));
    Prob(SelDummyAge) = InacProb(SelDummyAge);
    ProbInaction(RegionsUnobRep == r) = prod(Prob,2);
end

ProbUHUnobRep = p_uh_old*ProbInaction./(p_uh_old*ProbInaction + (1-p_uh_old));

UHprob = ones(size(AgeState,1),1);
UHprob(FieldsPossibleUnobRep) = ProbUHUnobRep;
UHprob(NeverRep) = 0;

p_uh_new = mean(UHprob(FieldsUH));